﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using sharp_net;
using sharp_net.Repositories;
using sharp_net.Specifications;
using zkdao.Domain;
using zkdao.Enum;

namespace zkdao.Application {

    public class ImageApplication : BaseApplication, IImageService {

        public ImageData Get(int ID) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var imgRepository = context.GetRepository<Image>();
                var img = imgRepository.GetById(ID);
                if (img == null)
                    return null;
                var imgData = Mapper.Map<Image, ImageData>(img);
                return imgData;
            }
        }

        public ImageData GetByPath(string path) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var imgRepository = context.GetRepository<Image>();
                var img = imgRepository.Find(Specification<Image>.Eval(c => c.Path == path));
                if (img == null)
                    return null;
                var imgData = Mapper.Map<Image, ImageData>(img);
                return imgData;
            }
        }

        public ImageData GetByHost(eImageHostType hostType, string hostId) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var imgRepository = context.GetRepository<Image>();
                var img = imgRepository.Find(Specification<Image>.Eval(c => c.HostTypeEnum == (int)hostType && c.HostID == hostId));
                if (img == null)
                    return null;
                var imgData = Mapper.Map<Image, ImageData>(img);
                return imgData;
            }
        }

        public ImageData Creat(ImageData dataObject, string userKey) {
            if (dataObject == null)
                throw new ArgumentNullException("imgDataObject");
            var userApplication = IocLocator.Instance.GetImple<IUserService>();
            UserData user = userApplication.GetByKey(userKey);
            dataObject.PosterID = user.ID;
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var imgRepository = context.GetRepository<Image>();
                Image img = Mapper.Map<ImageData, Image>(dataObject);
                img.Creat();
                imgRepository.Add(img);
                context.Commit();
                return Mapper.Map<Image, ImageData>(img);
            }
        }

        public void Update(ImageData dataObject) {
            if (dataObject.ID == 0)
                throw new ArgumentNullException("ID");
            Image replyChild = Mapper.Map<ImageData, Image>(dataObject);
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Image>();
                var upImage = tagRepository.Get(Specification<Image>.Eval(c => c.Id == dataObject.ID));
                upImage.Update(dataObject);
                tagRepository.Update(upImage);
            }
        }
    }
}